/**
 * 
 */
package com.globallogic.training.university;

/**
 * A <code>ScheduledTime</code> class represents a time of a lecture
 * 
 * @author vasiliy.kulgaviy
 * 
 */
public class ScheduledTime implements Comparable<ScheduledTime> {

	private WeekDay weekday;
	private int hour24;

	private static final int[] SCHEDUALABLE_HOURS = { 9, 11, 15, 17 };

	/**
	 * Constructs a Scheduled Time with given Week Day and Hour
	 * 
	 * @param weekday
	 *            Week Day of the new Scheduled Time
	 * @param hour24
	 *            Hour of the new Scheduled Time in 24h format
	 */
	public ScheduledTime(WeekDay weekday, int hour24) {
		this.weekday = weekday;
		this.hour24 = hour24;
	}

	/**
	 * Returns the weekday of the Scheduled Time
	 * 
	 * @return the weekday of the Scheduled Time
	 */
	public WeekDay getWeekday() {
		return weekday;
	}

	/**
	 * Returns the hour of the Scheduled Time
	 * 
	 * @return the hour of the Scheduled Time
	 */
	public int getHour24() {
		return hour24;
	}

	/**
	 * Returns hours that are possible to schedule
	 * 
	 * @return array of the schedulable hours
	 */
	public static int[] getSchedulableHours() {
		return SCHEDUALABLE_HOURS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object other) {
		if (other == null)
			return false;

		if (this == other) {
			return true;
		}

		if (!(other instanceof ScheduledTime)) {
			return false;
		}

		ScheduledTime otherScheduledTime = (ScheduledTime) other;

		if (hour24 != otherScheduledTime.hour24
				|| weekday != otherScheduledTime.weekday) {
			return false;
		}

		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(ScheduledTime otherScheduledTime) {

		int result = weekday.compareTo(otherScheduledTime.weekday);

		if (result != 0)
			return result;

		result = hour24 - otherScheduledTime.hour24;

		if (result != 0)
			return (int) (result / Math.abs(result));

		return 0;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 17;
		int result = 37;

		result = prime * result + hour24;
		result = prime * result + weekday.hashCode();

		return result;
	}
}
